import win32gui
import ctypes


def find_windows(name):
    isLike = True  # 是否模糊匹配
    if name[0] == "$":  # $开头表示精确匹配
        isLike = False
        name = name[1:]

    if isLike:
        winHand = None
        nameList = []
        def get_all_hwnd(hwnd, a):
            nonlocal winHand
            if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
                text = win32gui.GetWindowText(hwnd)
                if text != "":
                    nameList.append(text)
                    if name in text:
                        winHand = hwnd

        win32gui.EnumWindows(get_all_hwnd, 0)
        print("find_windows: ", nameList)
        return winHand
    else:
        return win32gui.FindWindow(None, name)


def get_dpi():
    dpi = ctypes.windll.user32.GetDpiForSystem()
    return dpi / 96
